package org.example.flow.controller;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.flow.domain.*;
import org.example.flow.service.*;
import org.example.flow.util.Result;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

@RequestMapping("/shop")
@RestController
@Slf4j
public class ShopController {
    @Autowired
    BusinessService businessService;
    @Autowired
    FlowService flowService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private MarketService marketService;
    @Autowired
     private  TflowService tflowService;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @RequestMapping("/shoplist")
    public Result shoplist(){
        List<Business> list = businessService.list();
        return Result.success(list);
    }
    @RequestMapping("/bussnass")
    public CompletableFuture<Result> bussnass(Integer id){
        CompletableFuture<Result> resultCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Business byId = businessService.getById(id);
            return Result.success(byId);
        }, threadPoolTaskExecutor);
        return resultCompletableFuture;
    }
    @RequestMapping("/bussnass2")
    public Result bussnass2(Integer id){
            Business byId = businessService.getById(id);
            return Result.success(byId);
    }
    @RequestMapping("/shopadd")
    public  CompletableFuture<Result> flowadd(@RequestBody Flow flow){
        CompletableFuture<Result> completableFuture = CompletableFuture.supplyAsync(() -> {
            log.info("===========线程执行了任务");
            LambdaQueryWrapper<Flow> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Flow::getId, flow.getId());
            Flow one = flowService.getOne(wrapper);
            one.setBusinessId(flow.getBusinessId());
            one.setStatus("开始运输");
            boolean b = flowService.updateById(one);
            if (b) {
                Business business = businessService.getById(flow.getBusinessId());
                Tflow tflow = new Tflow();
                tflow.setFId(flow.getId());
                tflow.setFTime(new Date());
                tflow.setFLatitude(business.getLatitude());
                tflow.setFLongitude(business.getLongitude());
                tflowService.save(tflow);
            }
            return b ? Result.success() : Result.fail();
        });
        return completableFuture;
    }
    @RequestMapping("getLineArr")
    public List<List<Double>> getLineArr(String id){
        List<List<Double>> lineArr = new ArrayList<>();
        List<Tflow> list=tflowService.getListById(id);
        System.out.println(list+"------------TDengine");
        for (Tflow tflow : list) {
            lineArr.add(Arrays.asList(tflow.getFLongitude(),tflow.getFLatitude()));
        }
        System.out.println(lineArr);
        return lineArr;

    }


//    @RequestMapping("getLineArr")
//    public Result getLineArr(@RequestBody Flow flow){
//        Market market = marketService.getById(flow.getMarketId());
//        Business business = businessService.getById(flow.getBusinessId());
//        Dept dept = deptService.getById(flow.getDeptId());
//
//        List<List<String>> list = new ArrayList<>();
//        list.add(Arrays.asList(dept.getLongitude(),dept.getLatitude()));
//        list.add(Arrays.asList(market.getLongitude(),market.getLatitude()));
//        list.add(Arrays.asList(business.getLongitude(),market.getLatitude()));
//        return Result.success(list);
//    }
    @Autowired
    ShopService shopService;
    @RequestMapping("getregular")
    public Result getregular(String id) throws InterruptedException {
        Thread.sleep(5000);
        LambdaQueryWrapper<Flow> flowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        flowLambdaQueryWrapper.eq(Flow::getId,id);
        Flow flow = flowService.getOne(flowLambdaQueryWrapper);
        flow.setStatus("已送达");
        boolean b=flowService.updateById(flow);
        LambdaQueryWrapper<Shop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Shop::getShopname,flow.getProductionName());
        wrapper.eq(Shop::getBusinessId,flow.getBusinessId());
        Shop one = shopService.getOne(wrapper);
        if(one==null){
            Shop shop = new Shop();
            String s = IdUtil.getSnowflake().nextIdStr();
            shop.setOutTradeNo(s);
            shop.setShopname(flow.getProductionName());
            shop.setStatus("还有存货");
            shop.setNum(flow.getProductionNum());
            double v = RandomUtil.randomDouble(50.00, 100.00);
            shop.setPrice(new BigDecimal(v));
            shop.setCreatTime(new Date());
            shop.setBusinessId(flow.getBusinessId());
            b = shopService.save(shop);
            return b?Result.success():Result.fail();
        }
        if(one.getStatus().equals("缺货中")){
            one.setStatus("还有存货");
        }
        one.setCreatTime(new Date());
        one.setNum(one.getNum()+flow.getProductionNum());
         b = shopService.updateById(one);
        return b?Result.success():Result.fail();
    }
    @RequestMapping("/getshopname")
    public Result getshopname(){
        List<Business> list = businessService.list();
        return Result.success(list);
    }
}
